package com.edu.dao;

import com.edu.entity.Food;
import com.edu.utils.JdbcHelper;
import com.edu.utils.PageList;
import com.edu.utils.PageParam;

import java.sql.SQLException;
import java.util.List;

/**
 * @author ZCN
 * @version 1.0
 * @date 2021/11/19 14:55
 */
public class FoodDao {

    // 分页数据
    public PageList pageList(PageParam pageParam) throws SQLException {
        String countsql = "select count(*) from food";
        String pagesql ="select * from food limit ?,?";
        Long count = (Long) JdbcHelper.getSingle(countsql);
        List data = JdbcHelper.query(pagesql, Food.class, pageParam.getStart(),pageParam.getLimit());
        return new PageList(count,data);
    }
    // 根据id查询
    public Food findOne(Integer id) throws SQLException {
        String sql = "select * from food where id = ?";
        List<Food> foods = JdbcHelper.query(sql, Food.class, id);
        if(foods.size() > 0){
            return foods.get(0);
        }
        return null;
    }


    // 查询所有
    public List<Food> findAll() throws SQLException {
        String sql = "select * from food";
        List<Food> list = JdbcHelper.query(sql,Food.class);
        return list;
    }
    // 今日推荐
    public List<Food> todayRecommend() throws SQLException {
        String sql = "select * from food where id in(6,1,5,3,8)";
        List<Food> todayFoods = JdbcHelper.query(sql, Food.class);
        return todayFoods;
    }
    // 热门菜品
    public List<Food> hotsFoods() throws SQLException {
        String sql = "select * from food where flag = 1 order by hits desc limit 5";
        List<Food> hotsFoods = JdbcHelper.query(sql, Food.class);
        return hotsFoods;
    }

    // 厨师推荐
    public List<Food> cookRecommend() throws SQLException {
        String sql = "select * from food where comment = 0 and flag = 1 limit 5";
        List<Food> cookrecommentFoods = JdbcHelper.query(sql, Food.class);
        return cookrecommentFoods;
    }

    // 热门菜品(全部,详情)
    public List<Food> hotsFoodsComplex() throws SQLException {
        String sql = "select * from food where flag = 1 order by hits desc";
        List<Food> hotsFoodscom = JdbcHelper.query(sql, Food.class);
        return hotsFoodscom;
    }

    // 厨师推荐
    public List<Food> cookRecommendComplex() throws SQLException {
        String sql = "select * from food where comment = 0 and flag = 1";
        List<Food> cookrecommentFoods = JdbcHelper.query(sql, Food.class);
        return cookrecommentFoods;
    }


    // 修改
    public void update(Food food) throws SQLException {
        String sql = "update food set " +
                "foodname=? , feature=? ," +
                "material=? ,price=? ," +
                "type=? ,picture=? ," +
                "comment=? ," +
                "typename=?,flag=?" +
                " where id=?";
        JdbcHelper.update(sql, food.getFoodname(),food.getFeature(),
                food.getMaterial(),food.getPrice(),
                food.getType(),food.getPicture(),
                food.getComment(),
                food.getTypename(),food.getFlag(),
                food.getId());
    }

    // 上架
    public void delete(Integer id) throws SQLException {
        String sql = "update food set flag = 0 where id = ?";
        JdbcHelper.update(sql,id);
    }

    // 上架
    public void online(Integer id) throws SQLException {
        String sql = "update food set flag = 1 where id = ?";
        JdbcHelper.update(sql,id);
    }

    // 添加
    public void insert(Food food) throws SQLException {
        String sql = "insert into food(" +
                "foodname," +
                "feature," +
                "material," +
                "price," +
                "type," +
                "picture," +
                "hits," +
                "comment," +
                "typename," +
                "flag"+
                ") " +
                "values (?,?,?,?,?,?,?,?,?,?)";
        JdbcHelper.update(sql,food.getFoodname(),food.getFeature(),food.getMaterial(),
                food.getPrice(),food.getType(),food.getPicture(),0,food.getComment(),
                food.getTypename(),1);
    }
}
